package com.woniu.market_stall.web;

import com.github.pagehelper.PageInfo;
import com.woniu.market_stall.entity.ManagerDept;
import com.woniu.market_stall.result.ResultResponse;
import com.woniu.market_stall.service.LogService;
import com.woniu.market_stall.vo.LogVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("log")
public class LogController {
    @Autowired
    LogService logService;

    @GetMapping("/list")
    @ApiOperation(value = "本接口用于分页查看日志")
    public ResultResponse<PageInfo<LogVo>> list(
            @ApiParam(value = "当前页的参数")  @RequestParam(value = "now",defaultValue = "1") Integer now,
            @ApiParam(value = "页面大小的参数") @RequestParam(value = "size",defaultValue = "3")Integer size,
            @ApiParam(value = "按条件搜索日志") String name
    ){
        ResultResponse<PageInfo<LogVo>> result = null;
        try{
            PageInfo<LogVo> pageInfo = logService.queryLogByPage(now, size, name);
            result = new ResultResponse<>(200,"查询日志成功!",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询日志失败!");
        }
        return result;
    }

    @ApiOperation(value = "本接口用于逻辑删除日志")
    @DeleteMapping("/del/{id}")
    public ResultResponse<Void> del(@PathVariable("id")Integer id){
        ResultResponse<Void> result = null;
        try {
            logService.removeByKey(id);
            result = new ResultResponse<>(200,"删除日志成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除日志失败!");
        }
        return result ;
    }

    @ApiOperation(value = "本接口用于批量删除日志")
    @PostMapping("/delBatch")
    public ResultResponse<Void> delBatch(@RequestBody String[] ids){
        ResultResponse<Void> result = null;
        try {
//            String[] split = ids.split(",");
//            System.out.println(split);
            for (String id : ids){
                logService.removeBatch(id);
           }
            result = new ResultResponse<>(200,"删除日志成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除日志失败!");
        }
        return result ;
    }

}
